<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Validation\Rule;
use App\ShopHair;
use App\Color;
use Input;
use DB;

class ShopColorController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        //需要有店铺的id
        $shopid = $request -> id;
        //获取已有的color
        $colorids = ShopHair::where([['shopid','=',$shopid],['type','=','1']])
        ->select('value')
        ->get()
        ->toArray();
        foreach($colorids as $colorid){
            $ids[] = $colorid['value'];
        }
        //选择出没有选中的hair_style
        $datas = Color::whereNotIn('colortypeid',$ids)
            ->select('colortypeid','samplethumb','colorname')
            ->get();
        //然后获取已经获取的数据
        return view('admin.shopcolor.add',compact('datas','shopid'));


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //首先进行验证
        $this -> validate($request,[
//            'value' => 'unique:shops_haircolor,value'
            'value' => Rule::unique('shops_haircolor')->where(function($query){$query -> where('type','1');})

        ],[
            'value.unique'=>'有重复添加的数据,请刷新页面,重新添加'
        ]);
        //获取shopid
        $shopid = Input::get('shopid');
        $type = '1';
        //进行存储
        $colortypeids = Input::get('value');
        //将数据批量插入
        foreach($colortypeids as $k=>$colortypeid){
            $data[$k] = ['type'=>$type,'shopid'=>$shopid,'value'=>$colortypeid];
        }
        $res = ShopHair::where('shopid',$shopid)->insert($data);
        if($res)
        return Redirect('shopcolor/'.$shopid)->with(['info'=>'添加成功']);
        return back()->with(['info'=>'添加失败']);


    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //根据id获得color
        //获取color列表
        $datas = DB::table('shops_haircolor')
            ->select('shops_haircolor.id','hair_colortype.colortypeid','hair_colortype.samplethumb','hair_colortype.colorname')
            ->Join('hair_colortype','hair_colortype.colortypeid','=','shops_haircolor.value')
            ->where([['shops_haircolor.type','=','1'],['shops_haircolor.shopid','=',$id]])
            ->paginate('20');
        return view('admin.shopcolor.index',compact('datas','id'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //获取id直接删除
        $res = ShopHair::destroy($id);
        if($res)
        return Response() -> json(['status'=>'success','msg'=>'删除成功']);
        return Response() -> json(['status'=>'error','msg'=>'删除失败']);
    }
}
